<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设计模式解析：观察者模式</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            line-height: 1.8;
            color: #333;
            background-color: #f9f9f9;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            color: #2d3748;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
        }
        .code-block {
            background: #282c34;
            border-left: 4px solid #667eea;
        }
        .highlight {
            position: relative;
        }
        .highlight::after {
            content: "";
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 10px;
            background: linear-gradient(90deg, rgba(102, 126, 234, 0.3) 0%, rgba(118, 75, 162, 0.3) 100%);
            z-index: -1;
        }
        .mermaid-container {
            background: white;
            padding: 2rem;
            border-radius: 0.5rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-4xl text-center">
            <div class="inline-block px-4 py-1 mb-4 text-xs font-semibold tracking-wider text-purple-100 bg-purple-800 rounded-full">设计模式解析</div>
            <h1 class="text-4xl md:text-6xl font-bold mb-6">观察者模式</h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-2xl mx-auto leading-relaxed">
                构建松耦合系统的关键 - 让对象间的状态变化自动传播
            </p>
            <div class="mt-10 flex justify-center space-x-4">
                <a href="#intro" class="px-6 py-3 bg-white text-purple-700 font-medium rounded-lg hover:bg-purple-50 transition duration-200">
                    <i class="fas fa-book-open mr-2"></i>了解模式
                </a>
                <a href="#example" class="px-6 py-3 bg-purple-800 text-white font-medium rounded-lg hover:bg-purple-700 transition duration-200">
                    <i class="fas fa-code mr-2"></i>查看示例
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-6xl px-4 py-12">
        <!-- Introduction Section -->
        <section id="intro" class="mb-20">
            <div class="bg-white rounded-xl p-8 shadow-lg">
                <h2 class="text-3xl font-bold mb-6 highlight">什么是观察者模式？</h2>
                <p class="text-lg text-gray-700 mb-6">
                    观察者模式（Observer Pattern）是一种行为型设计模式，用于建立对象之间的依赖关系，以便当一个对象的状态发生变化时，所有依赖于它的对象都能够自动更新。
                </p>
                <p class="text-lg text-gray-700">
                    它的核心思想是将一个对象（主题或被观察者）的状态变化通知所有依赖于它的对象（观察者），从而实现解耦和自动更新。
                </p>
                
                <!-- Diagram -->
                <div class="mt-10 mermaid-container">
                    <div class="mermaid">
                        classDiagram
                            class Subject {
                                +registerObserver(Observer o)
                                +removeObserver(Observer o)
                                +notifyObservers()
                            }
                            
                            class Observer {
                                <<interface>>
                                +update()
                            }
                            
                            class ConcreteSubject {
                                -state
                                +getState()
                                +setState()
                            }
                            
                            class ConcreteObserver {
                                -subject
                                +update()
                            }
                            
                            Subject <|-- ConcreteSubject
                            Observer <|-- ConcreteObserver
                            Subject "1" *-- "0..*" Observer : observers
                            ConcreteObserver o-- ConcreteSubject : subject
                    </div>
                </div>
            </div>
        </section>

        <!-- Components Section -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-center highlight">观察者模式的组成</h2>
            <div class="grid md:grid-cols-2 gap-8">
                <div class="card bg-white rounded-xl p-6">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-bullseye text-purple-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">主题（Subject）</h3>
                    </div>
                    <p class="text-gray-700 mb-4">
                        <strong>定义：</strong>被观察的对象，它的状态发生变化时，会通知所有注册的观察者。
                    </p>
                    <p class="text-gray-700">
                        <strong>职责：</strong>提供注册、注销观察者的方法，并在状态发生变化时通知所有观察者。
                    </p>
                </div>
                
                <div class="card bg-white rounded-xl p-6">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-eye text-blue-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">观察者（Observer）</h3>
                    </div>
                    <p class="text-gray-700 mb-4">
                        <strong>定义：</strong>依赖于主题对象的对象。当主题的状态变化时，观察者会自动更新自己。
                    </p>
                    <p class="text-gray-700">
                        <strong>职责：</strong>定义一个更新接口，以便在主题状态变化时接收通知。
                    </p>
                </div>
                
                <div class="card bg-white rounded-xl p-6">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-indigo-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-cube text-indigo-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">具体主题（ConcreteSubject）</h3>
                    </div>
                    <p class="text-gray-700 mb-4">
                        <strong>定义：</strong>实现了主题接口的具体类，维护主题的状态，并在状态变化时通知观察者。
                    </p>
                    <p class="text-gray-700">
                        <strong>职责：</strong>维护状态并实现通知机制。
                    </p>
                </div>
                
                <div class="card bg-white rounded-xl p-6">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-green-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-user text-green-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold text-gray-800">具体观察者（ConcreteObserver）</h3>
                    </div>
                    <p class="text-gray-700 mb-4">
                        <strong>定义：</strong>实现了观察者接口的具体类，响应主题的状态变化并做出相应的处理。
                    </p>
                    <p class="text-gray-700">
                        <strong>职责：</strong>定义对主题状态变化的响应行为。
                    </p>
                </div>
            </div>
        </section>

        <!-- Workflow Section -->
        <section class="mb-20">
            <div class="bg-white rounded-xl p-8 shadow-lg">
                <h2 class="text-3xl font-bold mb-8 highlight">工作原理</h2>
                
                <div class="grid md:grid-cols-3 gap-8">
                    <div class="text-center">
                        <div class="w-16 h-16 bg-yellow-100 rounded-full flex items-center justify-center mx-auto mb-4">
                            <span class="text-yellow-600 text-2xl font-bold">1</span>
                        </div>
                        <h3 class="text-xl font-semibold mb-2">注册观察者</h3>
                        <p class="text-gray-700">
                            观察者对象向主题注册自己，以便在主题状态变化时接收通知。主题维护一个观察者列表，记录所有注册的观察者。
                        </p>
                    </div>
                    
                    <div class="text-center">
                        <div class="w-16 h-16 bg-red-100 rounded-full flex items-center justify-center mx-auto mb-4">
                            <span class="text-red-600 text-2xl font-bold">2</span>
                        </div>
                        <h3 class="text-xl font-semibold mb-2">状态变化</h3>
                        <p class="text-gray-700">
                            当主题的状态发生变化时，主题会遍历观察者列表，并调用每个观察者的更新方法，将变化通知给观察者。
                        </p>
                    </div>
                    
                    <div class="text-center">
                        <div class="w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-4">
                            <span class="text-green-600 text-2xl font-bold">3</span>
                        </div>
                        <h3 class="text-xl font-semibold mb-2">更新观察者</h3>
                        <p class="text-gray-700">
                            观察者接收到通知后，根据主题的状态变化执行相应的操作。
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Example Section -->
        <section id="example" class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-center highlight">示例代码</h2>
            <div class="bg-white rounded-xl overflow-hidden shadow-lg">
                <div class="bg-gray-800 px-6 py-3 flex items-center">
                    <div class="flex space-x-2 mr-4">
                        <div class="w-3 h-3 rounded-full bg-red-500"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500"></div>
                    </div>
                    <div class="text-gray-300 text-sm">WeatherStation.java</div>
                </div>
                <div class="code-block p-6 overflow-x-auto">
                    <pre class="text-gray-200"><code>// 观察者接口
public interface Observer {
    void update(float temperature, float humidity, float pressure);
}

// 具体观察者
public class DisplayDevice implements Observer {
    @Override
    public void update(float temperature, float humidity, float pressure) {
        // 根据天气数据更新显示内容
        System.out.println("Temperature: " + temperature + "°C");
        System.out.println("Humidity: " + humidity + "%");
        System.out.println("Pressure: " + pressure + " hPa");
    }
}

// 主题接口
public interface Subject {
    void registerObserver(Observer o);
    void removeObserver(Observer o);
    void notifyObservers();
}

// 具体主题
public class WeatherStation implements Subject {
    private List&lt;Observer&gt; observers = new ArrayList&lt;&gt;();
    private float temperature;
    private float humidity;
    private float pressure;

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        observers.remove(o);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(temperature, humidity, pressure);
        }
    }

    // 设置天气数据并通知观察者
    public void setWeatherData(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        notifyObservers();
    }
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Pros & Cons Section -->
        <section class="mb-20">
            <div class="grid md:grid-cols-2 gap-8">
                <div class="card bg-white rounded-xl p-8">
                    <h2 class="text-3xl font-bold mb-6 text-green-600">
                        <i class="fas fa-check-circle mr-3"></i>优点
                    </h2>
                    <ul class="space-y-4">
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-3"></i>
                            <div>
                                <h4 class="font-semibold text-gray-800">解耦</h4>
                                <p class="text-gray-700">观察者模式将主题和观察者之间的耦合度降低，使得主题和观察者之间可以独立变化和扩展。</p>
                            </div>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-3"></i>
                            <div>
                                <h4 class="font-semibold text-gray-800">动态修改</h4>
                                <p class="text-gray-700">观察者可以在运行时动态地添加或移除，提供了灵活的响应机制。</p>
                            </div>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check text-green-500 mt-1 mr-3"></i>
                            <div>
                                <h4 class="font-semibold text-gray-800">自动更新</h4>
                                <p class="text-gray-700">主题状态发生变化时，所有观察者都能自动得到通知，并更新自身状态。</p>
                            </div>
                        </li>
                    </ul>
                </div>
                
                <div class="card bg-white rounded-xl p-8">
                    <h2 class="text-3xl font-bold mb-6 text-red-600">
                        <i class="fas fa-exclamation-triangle mr-3"></i>缺点
                    </h2>
                    <ul class="space-y-4">
                        <li class="flex items-start">
                            <i class="fas fa-times text-red-500 mt-1 mr-3"></i>
                            <div>
                                <h4 class="font-semibold text-gray-800">性能问题</h4>
                                <p class="text-gray-700">如果观察者列表较长，通知所有观察者可能会导致性能问题，特别是在观察者执行复杂操作时。</p>
                            </div>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times text-red-500 mt-1 mr-3"></i>
                            <div>
                                <h4 class="font-semibold text-gray-800">复杂性</h4>
                                <p class="text-gray-700">观察者模式引入了额外的复杂性，特别是在涉及多个观察者和主题的复杂系统中，调试和维护可能变得更加困难。</p>
                            </div>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-times text-red-500 mt-1 mr-3"></i>
                            <div>
                                <h4 class="font-semibold text-gray-800">内存泄漏</h4>
                                <p class="text-gray-700">如果观察者未被正确注销，可能会导致内存泄漏，因为主题仍然持有对观察者的引用。</p>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Real-world Applications -->
        <section class="mb-20">
            <div class="bg-white rounded-xl p-8 shadow-lg">
                <h2 class="text-3xl font-bold mb-8 highlight">实际应用场景</h2>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="flex items-start">
                        <div class="bg-blue-100 p-3 rounded-lg mr-4">
                            <i class="fas fa-bell text-blue-600 text-xl"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-lg mb-2">事件处理系统</h4>
                            <p class="text-gray-700">GUI组件中的事件监听机制，如按钮点击事件、键盘输入事件等。</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <div class="bg-purple-100 p-3 rounded-lg mr-4">
                            <i class="fas fa-rss text-purple-600 text-xl"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-lg mb-2">发布-订阅系统</h4>
                            <p class="text-gray-700">消息队列、实时数据推送、新闻订阅等场景。</p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <div class="bg-green-100 p-3 rounded-lg mr-4">
                            <i class="fas fa-chart-line text-green-600 text-xl"></i>
                        </div>
                        <div>
                            <h4 class="font-bold text-lg mb-2">数据监控系统</h4>
                            <p class="text-gray-700">当数据发生变化时，自动更新多个显示组件或触发相关操作。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-10 px-4">
        <div class="container mx-auto max-w-6xl">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-gray-400">探索编程之美，分享技术之道</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-purple-300 hover:text-white transition duration-200">
                        <i class="fas fa-external-link-alt mr-2"></i>www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-gray-500 text-sm">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>